// BOSS激光扫射类
import { TimeManager } from './TimeManager.js';

export class BossLaser {
    constructor(x, y, angle, length = 400) {
        this.x = x;
        this.y = y;
        this.angle = angle || Math.PI / 2; // 默认向下
        this.length = length;
        this.width = 36; // 激光宽度（加粗三倍）
        
        // 激光状态
        this.charging = false;
        this.active = false;
        this.chargeTime = 0;
        this.maxChargeTime = 1500; // 1.5秒充能时间
        this.activeTime = 0;
        this.maxActiveTime = 2000; // 2秒激光持续时间
        
        // 视觉特效
        this.chargePhase = 0;
        this.pulsePhase = 0;
        this.intensity = 0;
        
        // 伤害相关
        this.damageInterval = 200; // 200毫秒伤害一次
        this.lastDamageTime = 0;
    }
    
    update(deltaTime, newAngle = null) {
        // 更新角度（用于扫射）
        if (newAngle !== null) {
            this.angle = newAngle;
        }
        
        // 更新视觉特效
        this.chargePhase = TimeManager.updatePhase(this.chargePhase, deltaTime, 15);
        this.pulsePhase = TimeManager.updatePhase(this.pulsePhase, deltaTime, 20);
        
        // 充能阶段
        if (this.charging && !this.active) {
            this.chargeTime += deltaTime;
            this.intensity = Math.min(1, this.chargeTime / this.maxChargeTime);
            
            if (this.chargeTime >= this.maxChargeTime) {
                this.charging = false;
                this.active = true;
                this.activeTime = 0;
                this.intensity = 1;
            }
        }
        
        // 激活阶段
        if (this.active) {
            this.activeTime += deltaTime;
            
            // 激光强度在激活期间保持满值，但加入脉冲效果
            this.intensity = 0.9 + Math.sin(this.pulsePhase) * 0.1;
            
            if (this.activeTime >= this.maxActiveTime) {
                this.active = false;
                this.intensity = 0;
                return false; // 激光结束
            }
        }
        
        return true; // 激光继续
    }
    
    // 开始充能
    startCharging() {
        this.charging = true;
        this.active = false;
        this.chargeTime = 0;
        this.activeTime = 0;
        this.intensity = 0;
    }
    
    // 检查是否可以造成伤害
    canDamage() {
        const currentTime = Date.now();
        if (this.active && currentTime - this.lastDamageTime >= this.damageInterval) {
            this.lastDamageTime = currentTime;
            return true;
        }
        return false;
    }
    
    // 检查激光是否与矩形碰撞
    checkCollision(rect) {
        if (!this.active) return false;
        
        // 计算激光的起始点和结束点
        const startX = this.x;
        const startY = this.y;
        const endX = this.x + Math.cos(this.angle) * this.length;
        const endY = this.y + Math.sin(this.angle) * this.length;
        
        // 使用线段与矩形的碰撞检测
        return this.lineRectCollision(
            startX, startY, endX, endY,
            rect.x, rect.y, rect.width, rect.height
        );
    }
    
    // 线段与矩形碰撞检测
    lineRectCollision(x1, y1, x2, y2, rx, ry, rw, rh) {
        // 检查线段端点是否在矩形内
        if (this.pointInRect(x1, y1, rx, ry, rw, rh) || 
            this.pointInRect(x2, y2, rx, ry, rw, rh)) {
            return true;
        }
        
        // 检查线段是否与矩形的任一边相交
        return this.lineLineIntersection(x1, y1, x2, y2, rx, ry, rx + rw, ry) ||       // 上边
               this.lineLineIntersection(x1, y1, x2, y2, rx + rw, ry, rx + rw, ry + rh) || // 右边
               this.lineLineIntersection(x1, y1, x2, y2, rx + rw, ry + rh, rx, ry + rh) ||  // 下边
               this.lineLineIntersection(x1, y1, x2, y2, rx, ry + rh, rx, ry);             // 左边
    }
    
    // 点是否在矩形内
    pointInRect(x, y, rx, ry, rw, rh) {
        return x >= rx && x <= rx + rw && y >= ry && y <= ry + rh;
    }
    
    // 两线段是否相交
    lineLineIntersection(x1, y1, x2, y2, x3, y3, x4, y4) {
        const denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (denominator === 0) return false; // 平行线
        
        const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denominator;
        const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denominator;
        
        return t >= 0 && t <= 1 && u >= 0 && u <= 1;
    }
    
    render(ctx) {
        if (this.intensity <= 0) return;
        
        ctx.save();
        
        const endX = this.x + Math.cos(this.angle) * this.length;
        const endY = this.y + Math.sin(this.angle) * this.length;
        
        if (this.charging) {
            // 充能阶段：绘制充能指示器
            this.renderChargingEffect(ctx, endX, endY);
        }
        
        if (this.active) {
            // 激活阶段：绘制激光
            this.renderActiveLaser(ctx, endX, endY);
        }
        
        ctx.restore();
    }
    
    renderChargingEffect(ctx, endX, endY) {
        // 充能警告线
        const chargeIntensity = 0.3 + (this.intensity * 0.7);
        const pulse = 0.8 + Math.sin(this.chargePhase) * 0.2;
        
        ctx.strokeStyle = `rgba(255, 255, 0, ${chargeIntensity * pulse})`;
        ctx.lineWidth = 6; // 增加警告线宽度
        ctx.setLineDash([15, 8]); // 调整虚线样式
        
        ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(endX, endY);
        ctx.stroke();
        
        ctx.setLineDash([]); // 重置虚线
        
        // 充能粒子效果
        const particleCount = Math.floor(this.intensity * 12); // 增加粒子数量
        for (let i = 0; i < particleCount; i++) {
            const t = (i / particleCount) + (Date.now() % 1000) / 1000;
            const particleX = this.x + (endX - this.x) * (t % 1);
            const particleY = this.y + (endY - this.y) * (t % 1);
            
            ctx.fillStyle = `rgba(255, 255, 0, ${this.intensity * 0.8})`;
            ctx.beginPath();
            ctx.arc(particleX, particleY, 4, 0, Math.PI * 2); // 增加粒子大小
            ctx.fill();
        }
    }
    
    renderActiveLaser(ctx, endX, endY) {
        // 主激光光束
        const laserPulse = 0.9 + Math.sin(this.pulsePhase) * 0.1;
        
        // 外层光晕
        ctx.strokeStyle = `rgba(255, 100, 100, ${this.intensity * 0.6 * laserPulse})`;
        ctx.lineWidth = this.width + 8;
        ctx.lineCap = 'round';
        
        ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(endX, endY);
        ctx.stroke();
        
        // 中层激光
        ctx.strokeStyle = `rgba(255, 150, 150, ${this.intensity * 0.8 * laserPulse})`;
        ctx.lineWidth = this.width;
        
        ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(endX, endY);
        ctx.stroke();
        
        // 内层核心
        ctx.strokeStyle = `rgba(255, 255, 255, ${this.intensity * laserPulse})`;
        ctx.lineWidth = this.width / 3;
        
        ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(endX, endY);
        ctx.stroke();
        
        // 激光起点发光效果
        ctx.fillStyle = `rgba(255, 200, 200, ${this.intensity * 0.8})`;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.width * 0.8, 0, Math.PI * 2); // 调整为激光宽度的80%
        ctx.fill();
    }
}